package com.nenglai.common.resource;

import com.nenglai.common.crud.EditorBody;
import com.nenglai.common.crud.FormBody;
import com.nenglai.common.crud.RemoveBody;
import com.nenglai.common.crud.SearchBody;
import com.nenglai.common.response.ValidateException;
import com.nenglai.common.vo.SearchVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 公共资源管理器
 *
 * @author kongdehui
 * @date 2021/04/21 17:00
 **/
@Slf4j
@AllArgsConstructor
@Component
public class ResourceManager {

    /**
     * 资源管理器
     */
    private final ResourceContainer container;

    /**
     * 查询单条
     *
     * @param apiName
     *            接口名称
     * @param searchBody
     * @return java.lang.Object
     * @author kongdehui
     * @date 2021/04/22 14:01
     */
    public Object selectOne(String apiName, SearchBody searchBody) {
        if (StringUtils.isNotEmpty(apiName)) {
            return container.getResourceService(apiName).searchOne(searchBody);
        }
        return null;
    }

    /**
     * 查询列表（分页）
     *
     * @param apiName
     *            接口名称
     * @param searchBody
     *            查询body
     * @return java.lang.Object
     * @author kongdehui
     * @date 2021/04/21 22:26
     */
    public SearchVO select(String apiName, SearchBody searchBody) {
        if (StringUtils.isNotEmpty(apiName)) {
            return container.getResourceService(apiName).search(searchBody);
        }
        return null;
    }

    /**
     * 删除
     *
     * @param apiName
     * @param removeBody
     * @return java.lang.Boolean
     * @author kongdehui
     * @date 2021/04/22 17:29
     */
    public Boolean delete(String apiName, RemoveBody removeBody) {
        if (StringUtils.isNotEmpty(apiName)) {
            return container.getResourceService(apiName).delete(removeBody);
        }
        return null;
    }

    /**
     * 添加
     * 
     * @author kongdehui
     * @date 2021/04/22 22:46
     * @param apiName
     * @param formBody
     * @return java.lang.Object
     */
    public Object add(String apiName, FormBody formBody) throws ValidateException {
        if (StringUtils.isNotEmpty(apiName)) {
            return container.getResourceService(apiName).add(formBody);
        }
        return null;
    }

    /**
     * 更新
     * 
     * @author kongdehui
     * @date 2021/04/30 19:11
     * @param apiName
     * @param formBody
     * @return java.lang.Object
     */
    public Object update(String apiName, FormBody formBody) throws ValidateException {
        if (StringUtils.isNotEmpty(apiName)) {
            return container.getResourceService(apiName).update(formBody);
        }
        return null;
    }

    /**
     * 修改
     * 
     * @author kongdehui
     * @date 2021/04/30 19:42
     * @param apiName
     * @param editorBody
     * @return java.lang.Object
     */
    public Boolean edit(String apiName, EditorBody editorBody) throws ValidateException {
        if (StringUtils.isNotEmpty(apiName)) {
            return container.getResourceService(apiName).edit(editorBody);
        }
        return null;
    }

    /**
     * 配置信息
     * 
     * @author kongdehui
     * @date 2021/04/30 20:20
     * @param apiName
     * @return java.lang.Object
     */
    public Object configs(String apiName) throws ValidateException {
        if (StringUtils.isNotEmpty(apiName)) {
            return container.getResourceService(apiName).configs();
        }
        return null;
    }

}
